package cn.hanyx.coder.design.pattern.structural.composite.demo01;

import cn.hanyx.coder.design.pattern.structural.composite.demo01.dao.DepartmentRepo;
import cn.hanyx.coder.design.pattern.structural.composite.demo01.dao.EmployeeRepo;

import java.util.List;

/**
 * Demo
 *
 * @author hanyx
 * @date 2020/08/21
 */
public class Demo {
    private static final long ORGANIZATION_ROOT_ID = 1001;
    private DepartmentRepo departmentRepo = new DepartmentRepo(); // 依赖注入
    private EmployeeRepo employeeRepo = new EmployeeRepo(); // 依赖注入

    public void buildOrganization() {
        Department rootDepartment = new Department(ORGANIZATION_ROOT_ID);
        buildOrganization(rootDepartment);
        System.out.println(rootDepartment);
    }

    private void buildOrganization(Department department) {
        List<Long> subDepartmentIds = departmentRepo.getSubDepartmentIds(department.getId());
        for (Long subDepartmentId : subDepartmentIds) {
            Department subDepartment = new Department(subDepartmentId);
            department.addSubNode(subDepartment);
            buildOrganization(subDepartment);
        }
        List<Long> employeeIds = employeeRepo.getDepartmentEmployeeIds(department.getId());
        for (Long employeeId : employeeIds) {
            double salary = employeeRepo.getEmployeeSalary(employeeId);
            department.addSubNode(new Employee(employeeId, salary));
        }
    }

    public static void main(String[] args) {
        Demo demo = new Demo();
        demo.buildOrganization();
    }
}
